<?php
/**
 * Invite_service.php
 * 邀请码
 * @author  :  Skiychan <dev@skiy.net>
 * @link    :  http://www.zzzzy.com
 * @created :  2016/2/25
 * @modified:
 * @version :  0.0.1
 */

class Invite_service extends MY_Service {
    private $_api_key = null;
    private $_api_secret = null;
    private $_invite_url = null;

    public function __construct() {
        parent::__construct();
        $this->_api_key = $this->config->item('invite_key');
        $this->_api_secret = $this->config->item('invite_secret');
        $this->_invite_url = $this->config->item('invite_url');
        $this->load->library('Requests');
        Requests::register_autoloader();
    }

    public function check($code) {
        $options['timeout'] = $this->_timeout();
        $url = $this->_invite_url."occupy";
        $resp = Requests::put($url, array('Accept' => 'application/json'), $this->data($code), $options);

        return json_decode($resp->body, true);
    }

    public function code($code) {
        $options['timeout'] = $this->_timeout();
        $url = $this->_invite_url.'verify?'.http_build_query($this->data($code));
        $resp = Requests::get($url, array('Accept' => 'application/json'), $options);
        return json_decode($resp->body, true);
    }

    private function data($code) {
        $data = array(
            'code' => $code,
            'sign' => md5($this->_api_secret.$code),
            'api_key' => $this->_api_key,
        );
        return $data;
    }

    private function _timeout() {
        return $this->config->item('invite_timeout');
    }
}